﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TriplesGameLogic;

namespace TriplesGameDemo
{
    class GameDemo
    {
        static void Main(string[] args)
        {
            char[] matrix = InitialMatrix();
            TriplesGame tg = new TriplesGame(matrix);
            for (char currentPlayer = 'A'; !tg.IsGameOver;
                matrix = tg.Matrix,
                tg = new TriplesGame(matrix),
                currentPlayer = currentPlayer == 'A' ? 'B' : 'A')
            {
                PrintMatrix(matrix);
                Console.WriteLine("\nPlayer {0}\'s turn:", currentPlayer);
                if (!tg.CanPlayerMove(currentPlayer))
                {
                    Console.WriteLine("There is no valid move!");
                    continue;
                }
                int x1, x2, y1, y2;
                ReadInput(out x1, out y1, out x2, out y2);
                if (!tg.TryMakeMove(currentPlayer, x1, y1, x2, y2))
                {
                    Console.WriteLine("Invalid move!");
                    currentPlayer = currentPlayer == 'A' ? 'B' : 'A';
                    continue;
                }
            }

            PrintGameResult(tg);
        }

        private static char[] InitialMatrix()
        {
            char[] matrix = new char[50];
            for (int i = 0; i < 50; i++)
            {
                matrix[i] = ' ';
            }
            return matrix;
        }

        private static void PrintMatrix(char[] matrix)
        {
            for (int i = 0; i < matrix.Length; i++)
            {
                Console.Write(matrix[i]);
                if ((i + 1) % 10 == 0)
                {
                    Console.WriteLine();
                }
            }
        }

        private static void ReadInput(out int x1, out int y1, out int x2, out int y2)
        {
            string input = Console.ReadLine();
            string[] numbers = input.Split(' ');
            x1 = int.Parse(numbers[0]);
            y1 = int.Parse(numbers[1]);
            x2 = int.Parse(numbers[2]);
            y2 = int.Parse(numbers[3]);
        }

        private static void PrintGameResult(TriplesGame tg)
        {
            int scoreA = tg.GetPlayerScore('A');
            int scoreB = tg.GetPlayerScore('B');
            Console.WriteLine("Score {0}:{1}", scoreA, scoreB);
            if (scoreA > scoreB)
            {
                Console.WriteLine("A wins!");
            }
            else if (scoreA < scoreB)
            {
                Console.WriteLine("B wins!");
            }
            else
            {
                Console.WriteLine("The game is a tie.");
            }
        }
    }
}
